from sakucrm import models
from stack.service.stack import StackConfig
from stack.service.stack import get_choice_text
from stack.service.stack import Option
from django import forms
from django.shortcuts import HttpResponse
from django.urls import reverse
from django.conf import settings
from django.db import transaction
from django.utils.safestring import mark_safe

class CustomerConfig(StackConfig):
    list_display = ['id','name','qq',get_choice_text('status','状态'),get_choice_text('gender','性别'),get_choice_text('source','来源')]
    order_by = ['-id']
    search_list = ['name','qq']
    filter_list = [Option('status',is_choice=True),Option('gender',is_choice=True),Option('source',is_choice=True)]

class PubCustomerModelForm(forms.ModelForm):
    class Meta:
        model=models.Customer
        exclude=['status','consultant']

class PublicCustomerConfig(StackConfig):
    list_display = [
        'name',
        'qq',
        get_choice_text('status','状态'),
        get_choice_text('gender','性别'),
        get_choice_text('source','来源')
    ]

    order_by = ['-id',]

    search_list = ['name','qq']

    model_form_class = PubCustomerModelForm

    def get_list_display(self):
        val_list=super().get_list_display()
        val_list.remove(StackConfig.display_edit_del_display)
        val_list.append(StackConfig.display_edit)
        val_list.insert(0,StackConfig.display_checkbox)
        return val_list

    def get_queryset(self):
        '''
        重写获取集合的方法 在公共客户中不显示 私户

        :return:
        '''
        return self.model_class.objects.filter(consultant__isnull=True)
    def multi_apply(self,request):
        '''
        批量申请客户
        :param request:
        :return:
        '''
        id_list=request.POST.getlist('pk')  #[1,2,3]  获取 客户id列表

        #
        current_user_id = 1  # ######  这以后要到session 中获取数据
        my_customer_count=models.Customer.objects.filter(consultant_id=current_user_id,status=2).count()
        if (my_customer_count+len(id_list)) > settings.MAX_PRIVATE_CUSTOMER:
            return HttpResponse("超过最大客户量")
        flag=False
        # ### 数据库的事务操作
        # with transaction.atomic():
        #     origin=models.Customer.objects.filter(id__in=id_list)
        #     print(origin)
            # if len(origin)==len(id_list):
            #     models.Customer.objects.filter(id__in=id_list).update(consultant_id=current_user_id)
            #     flag=True
        # if not flag:
        #     return HttpResponse('已被其他销售抢了')
    multi_apply.text = '申请客户'
    action_list = [multi_apply,]
class PriCustomoerModelForm(forms.ModelForm):
    '''
    重写私户的 modelform  使其不能分配销售
    '''
    class Meta:
        model=models.Customer
        exclude=['consultant',]


class PrivateCustomerConfig(StackConfig):

    def display_follow(self,row=None,header=False):
        if header:
            return '跟进记录'
        url=reverse('stack:sakucrm_consultrecord_pri_list')
        return mark_safe('<a href="{}?cid={}">跟进记录</a>'.format(url,row.pk))

    list_display = [
        'name',
        'qq',
        get_choice_text('status','状态'),
        get_choice_text('gender','性别'),
        get_choice_text('source','来源'),
        display_follow,
    ]

    order_by = ['-id',]

    search_list = ['name','qq']
    model_form_class = PriCustomoerModelForm   #

    # def get_list_display(self):
    #     '''
    #     重写显示列表的方法 去掉私户的删除功能
    #     :return:
    #     '''
    #     val_list=super().get_list_display()
    #     # val_list.remove(StackConfig.display_edit_del_display)
    #     # val_list.append(StackConfig.display_edit)
    #     return val_list
    def get_queryset(self):
        '''
        重写获取数据集合的方法
        :return:
        '''
        current_user_id=1#  ######  这以后要到session 中获取数据
        return self.model_class.objects.filter(consultant_id=current_user_id)

    def save(self,form,modify=False):
        '''
        重写数据的保存方法
        :param form:
        :param modify:
        :return:
        '''
        current_user_id = 1  # ######  这以后要到session 中获取数据
        form.instance.consultant = models.UserInfo.objects.get(id=current_user_id)  #添加新的客户的时候 该客户的 课程顾问 是自己的私户
        return form.save()